<template>
  <div class="wrap">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card区域 -->
    <el-card class="box-card">
      <!-- 添加 -->
      <el-row>
        <el-col :span="9">
          <el-button type="primary" @click="addGoodsCate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="cateList"
        :columns="columns"
        show-index
        max-height="600"
        index-text="#"
        border
        :expand-type="false"
        :selection-type="false"
        class="treeTable"
        :show-row-hover="false"
      >
        <template slot="isOk" slot-scope="scope">
          <i v-if="scope.row.cat_deleted === false" class="el-icon-circle-check" style="color:blue;font-size:20px" />
          <i v-else class="el-icon-circle-close" style="color:red;font-size:20px" />
        </template>
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level ===1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="operate" slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editCate(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="deleteCa(scope.row)">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="queryInfo.pagenum"
        :page-size="queryInfo.pagesize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
      <!-- 添加分类弹出框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateVisible"
        width="30%"
        @close="addCateCancle()"
      >
        <!-- 添加分类表单 -->
        <el-form ref="cateRef" :model="addCateForm" label-width="85px" :rules="addCateRules">
          <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="addCateForm.cat_name" />
          </el-form-item>
          <el-form-item label="父级分类:">
            <el-cascader
              v-model="CateId"
              :options="faCateDate"
              :props="cataDateOption"
              class="cascader"
              style="width:100%"
              clearable
              change-on-select
              @change="faCateChange"
            />
          </el-form-item>
        </el-form>
        <!-- 表单底部 -->
        <div slot="footer" class="dialog-footer">
          <el-button @click="addCateCancle">取 消</el-button>
          <el-button type="primary" @click="addCateComfirm">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑分类的弹出框 -->
      <el-dialog
        title="编辑分类"
        :visible.sync="editCateVisible"
        width="30%"
        @close="editCateCancle()"
      >
        <el-form ref="editCateRef" :model="editCateForm" label-width="85px" :rules="editCateRules">
          <el-form-item label="分类名称:" prop="cat_name">
            <el-input v-model="editCateForm.cat_name" />
          </el-form-item>
        </el-form>
        <!-- 表单底部 -->
        <div slot="footer" class="dialog-footer">
          <el-button @click="editCateCancle">取 消</el-button>
          <el-button type="primary" @click="editCateComfirm">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { getGoodCateList, addCategories, deleteCate, editCate } from '@/api/cate.js';
export default {
  name: 'Category',
  components: { },
  data() {
    return {
      editCateRules: {
        cat_name: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      editCateVisible: false, // 编辑分类是否可见
      // 编辑分类的form
      editCateForm: {
        cat_name: ''
      },
      // 商品分类级联选择配置项
      cataDateOption: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: true
      },
      CateId: [], // 父级分类选中的id
      // 父级分类数据
      faCateDate: [],
      // 添加分类表单验证
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      // 添加分类数据
      addCateForm: {
        // 添加分类的名称
        cat_name: '',
        // 父级分类id
        cat_pid: 0,
        // 分类的等级，默认要添加的是1级分类 0一级 1 二级  2三级
        cat_level: 0
      },
      // 添加分类弹出框是否可见
      addCateVisible: false,
      // 查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      // 商品分类列表
      cateList: [],
      // 总条数
      total: 0,
      // tree-table 配置项
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 将当前列定义为模板
          type: 'template',
          // 当前列模板名称
          template: 'isOk'
        },
        {
          label: '排序',
          // 将当前列定义为模板
          type: 'template',
          // 当前列模板名称
          template: 'sort'
        },
        {
          label: '操作',
          // 将当前列定义为模板
          type: 'template',
          // 当前列模板名称
          template: 'operate'
        }
      ],
      editCat_id: ''// 分类级别的id
    };
  },
  mounted() {
    this.getCateList();
    this.getGoodCateListR();
  },
  methods: {
    // 获取父级分类的数据（因为最多只有三级，所以，我们只获取到前两级就可以）
    getGoodCateListR() {
      getGoodCateList({ type: 2 }).then((res) => {
        const { data, meta } = res.data;
        if (meta.status !== 200) return this.message.error(meta.msg);
        this.faCateDate = data;
      });
    },
    // 编辑分类的取消
    editCateCancle() {
      this.editCateVisible = false;
      this.$refs.editCateRef.resetFields();
    },
    // 编辑分类的确定
    editCateComfirm() {
      this.$refs.editCateRef.validate((valid) => {
        if (!valid) return false;
        const cat_name = this.editCateForm.cat_name;
        editCate(this.editCat_id, { cat_name }).then((res) => {
          const { meta } = res.data;
          if (meta.status !== 200) return this.$message.error(meta.msg);
          this.$message.success(meta.msg);
          this.getCateList();
          this.editCateVisible = false;
        });
      });
    },
    // 删除分类
    deleteCa(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCate(row.cat_id).then((res) => {
          const { status, msg } = res.data.meta;
          if (status !== 200) return this.$message.error(msg);
          this.getCateList();
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 添加分类取消
    addCateCancle() {
      this.addCateVisible = false;
      this.$refs.cateRef.clearValidate();
      // 添加分类数据
      this.addCateForm = {
        // 添加分类的名称
        cat_name: '',
        // 父级分类id
        cat_pid: 0,
        // 分类的等级，默认要添加的是1级分类 0一级 1 二级  2三级
        cat_level: 0
      };
      this.CateId = [];
    },
    // 添加分类确定
    addCateComfirm() {
      this.$refs.cateRef.validate((valid) => {
        if (!valid) return false;
        addCategories(this.addCateForm).then((res) => {
          const { meta } = res.data;
          if (meta.status !== 201) return this.$message.error(meta.msg);
          this.$message.success(meta.msg);
          this.getCateList();
          this.addCateVisible = false;
        }).catch((err) => {
          console.log(err);
        });
      });
    },
    // 父级分类选中时触发(它返回的是一个数组)
    faCateChange(changeId) {
      // 父级分类id
      this.addCateForm.cat_pid = changeId.length > 0 ? changeId[changeId.length - 1] : 0;
      // 分类的等级
      this.addCateForm.cat_level = changeId.length;
    },
    // 添加商品分类
    addGoodsCate() {
      this.addCateVisible = true;
    },
    // 编辑商品分类
    editCate(row) {
      this.editCateVisible = true;
      this.editCat_id = row.cat_id;
      this.editCateForm.cat_name = row.cat_name;
    },
    // 当前页改变时触发
    handleCurrentChange(page) {
      this.queryInfo.pagenum = page;
      this.getCateList();
    },
    // 获取商品分类类表
    getCateList() {
      getGoodCateList(this.queryInfo).then((res) => {
        const { data, meta } = res.data;
        if (meta.status !== 200) return this.$message.error(meta.msg);
        this.cateList = data.result;
        this.total = data.total;
      }).catch((err) => {
        console.log(err);
      });
    }
  }
};
</script>
<style scoped lang="scss">
.treeTable{
   margin:15px 0;
    max-height:600px;
    overflow-y:auto;
}

</style>
